package org.keyroy.mapEditor.ui.views.layouts;

import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Layout;

public class CenterLayout extends Layout {

	@Override
	protected Point computeSize(Composite composite, int wHint, int hHint,
			boolean flushCache) {
		int width = wHint;
		int height = wHint;
		Control[] controls = composite.getChildren();
		if (controls != null && controls.length > 0) {
			Control control = controls[0];
			Point cSzie = control.getSize();
			width = Math.max(wHint, cSzie.x);
			height = Math.max(hHint, cSzie.y);
		}

		return new Point(width, height);
	}

	@Override
	protected void layout(Composite composite, boolean flushCache) {
		Control[] controls = composite.getChildren();
		if (controls != null && controls.length > 0) {
			Control control = controls[0];
			Point size = composite.getSize();
			Point cSzie = control.getSize();
			control.setBounds((size.x - cSzie.x) / 2, (size.y - cSzie.y) / 2,
					cSzie.x, cSzie.y);
		}
	}

}
